﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.Model.RawDict;
using StoragePeriod.DBUtility;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.IDAL.RawDict;

namespace StoragePeriod.SqlServerDAL.RawDict
{
    /// <summary>
    /// 原料大类 操作
    /// </summary>
    public class D_RawMaterials : I_RawMaterials
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RawMaterials");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取符合要求的原料分类信息
        /// </summary>
        public List<E_RawMaterials> GetModelListByWhere(E_RawMaterials eRawMaterialsInfo)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from RawMaterials ");
            if (eRawMaterialsInfo.StrWhere != "")
            {
                sbSql.Append(" where " + eRawMaterialsInfo.StrWhere);
            }
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_RawMaterials> EntityList = new ModelHandler<E_RawMaterials>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(E_RawMaterials model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RawMaterials(");
            strSql.Append("id,TypeName,Back,temp1,temp2)");
            strSql.Append(" values (");
            strSql.Append("@id,@TypeName,@Back,@temp1,@temp2)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.TypeName;
            parameters[2].Value = model.Back;
            parameters[3].Value = model.temp1;
            parameters[4].Value = model.temp2;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_RawMaterials model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RawMaterials set ");
            strSql.Append("TypeName=@TypeName,");
            strSql.Append("Back=@Back,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeName", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.TypeName;
            parameters[1].Value = model.Back;
            parameters[2].Value = model.temp1;
            parameters[3].Value = model.temp2;
            parameters[4].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterials ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterials ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_RawMaterials GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,TypeName,Back,temp1,temp2 from RawMaterials ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            E_RawMaterials model = new E_RawMaterials();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeName"] != null)
                {
                    model.TypeName = ds.Tables[0].Rows[0]["TypeName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null)
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null)
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

    }
}
